#include "modifymembercardpassword.h"
#include "ui_modifymembercardpassword.h"
#include "inquiresql.h"
#include <QMessageBox>
#include <QKeyEvent>

ModifyMembercardPassword::ModifyMembercardPassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModifyMembercardPassword)
{
    ui->setupUi(this);
    this->setFixedSize(322,187);
    setWindowTitle(tr("修改会员卡密码"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    enforcement = false;
    m_lastClick = -1;

    //添加时间过滤器
    ui->lineEditOriginalPassword->installEventFilter(this);
    ui->lineEditNewPassword->installEventFilter(this);
    ui->lineEditConfirmPassword->installEventFilter(this);
    ui->pushButtonModify->installEventFilter(this);
}

ModifyMembercardPassword::~ModifyMembercardPassword()
{
    delete ui;
}

void ModifyMembercardPassword::displayMembercardID(const QString &membercardID)
{
    ui->lineEditMembercardID->setText(membercardID);
    ui->lineEditOriginalPassword->setReadOnly(false);
    ui->lineEditOriginalPassword->setFocus();
    ui->checkBoxForcedToModifyPassword->setCheckState(Qt::Unchecked);
    enforcement = false;
}

bool ModifyMembercardPassword::eventFilter(QObject *target, QEvent *event)
{
    if ( target == ui->lineEditOriginalPassword || target == ui->lineEditNewPassword
      || target == ui->lineEditConfirmPassword || target == ui->pushButtonModify )
    {
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditOriginalPassword)
                {
                    if (m_lastClick != lineEditOriginalPassword) {
                        ui->lineEditOriginalPassword->selectAll();
                        m_lastClick = lineEditOriginalPassword;
                        return true;
                    }else if (m_lastClick == lineEditOriginalPassword)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditNewPassword)
                {
                    if (m_lastClick != lineEditNewPassword){
                        ui->lineEditNewPassword->selectAll();
                        m_lastClick = lineEditNewPassword;
                        return true;
                    }else if (m_lastClick == lineEditNewPassword)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditConfirmPassword)
                {
                    if (m_lastClick != lineEditConfirmPassword){
                        m_lastClick = lineEditConfirmPassword;
                        ui->lineEditConfirmPassword->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditConfirmPassword)
                    {
                        goto flag;
                    }
                }
            }
        }

        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditOriginalPassword)
                {
                    ui->lineEditNewPassword->setFocus();
                    ui->lineEditNewPassword->selectAll();
                    return true;
                }
                if (target == ui->lineEditNewPassword)
                {
                    ui->lineEditConfirmPassword->setFocus();
                    ui->lineEditConfirmPassword->selectAll();
                    return true;
                }
                if (target == ui->lineEditConfirmPassword)
                {
                    ui->pushButtonModify->setFocus();
                    return true;
                }
                if (target == ui->pushButtonModify)
                {
                    on_pushButtonModify_clicked();
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}


void ModifyMembercardPassword::on_pushButtonModify_clicked()
{
    QString paswd = m_sql->inquireMemberPassword(ui->lineEditMembercardID->text());

    if (paswd != ui->lineEditOriginalPassword->text() && enforcement == false)
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n原始密码错误,请重新输入!"));
        ui->lineEditOriginalPassword->setFocus();
        ui->lineEditOriginalPassword->setText(NULL);
        return;
    }
    if (ui->lineEditNewPassword->text().isEmpty())
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n请输入新设密码!"));
        ui->lineEditNewPassword->setFocus();
        ui->lineEditNewPassword->setText(NULL);
        return;
    }
    if (ui->lineEditConfirmPassword->text().isEmpty())
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n请输入确认密码!"));
        ui->lineEditConfirmPassword->setFocus();
        ui->lineEditConfirmPassword->setText(NULL);
        return;
    }
    if (ui->lineEditConfirmPassword->text() != ui->lineEditNewPassword->text())
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n新设密码与确认密码不一致,请核对!"));
        ui->lineEditConfirmPassword->setFocus();
        ui->lineEditConfirmPassword->setText(NULL);
        return;
    }
    //修改密码
    if (!m_sql->UpdateMemberPassword(ui->lineEditMembercardID->text(),
                                ui->lineEditNewPassword->text()))
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n修改密码失败!"));
    else
        //成功修改后隐藏该窗口
        this->hide();
}

void ModifyMembercardPassword::on_pushButtonCancel_clicked()
{
    this->hide();
}


void ModifyMembercardPassword::on_checkBoxForcedToModifyPassword_clicked(bool checked)
{
    if (checked == true)
    {
        ui->lineEditOriginalPassword->setReadOnly(true);
        enforcement = true;
        ui->lineEditNewPassword->setFocus();
    }
    else
    {
        ui->lineEditOriginalPassword->setReadOnly(false);
        enforcement = false;
        ui->lineEditOriginalPassword->setFocus();
    }
}
